#include "message.pb.h"
#include <brpc/channel.h>

int main(int argc,char*argv[])
{
    //1.构造一个信道，连接服务器
    brpc::ChannelOptions options;
    options.connect_timeout_ms = -1;    // 连接等待超时时间，-1表示一直等待
    options.timeout_ms = -1;            //rpc请求等待超时时间，-1表示一直等待
    options.max_retry = 3;              //请求重试次数
    options.protocol = "baidu_std";     //序列化协议，默认使用baidu_std
    brpc::Channel channel;
    int ret = channel.Init("127.0.0.1:8080",&options);
    if (ret == -1) {
        std::cout << "初始化信道失败！\n";
        return -1;
    }
    //2.构造EchoService_Stub对象，用于进行rpc调用
    my::EchoService_Stub stub(&channel);

    //3.进行rpc调用
    my::EchoRequest req;
    req.set_message("你好rpc!!!!!");

    //4.添加上下文管理对象
    brpc::Controller *controller = new brpc::Controller();
    my::EchoResponse* rsp = new my::EchoResponse();

    stub.Echo(controller,&req,rsp,nullptr);
    if(controller->Failed()){
        std::cout << "Rpc调用失败:" << controller->ErrorText() << std::endl;
        return -1;
    }

    std::cout << "收到响应:" << rsp->message() << std::endl;
    delete controller;
    delete rsp;
    return 0;
}